package question.solution49;
import	java.util.ArrayList;

import question.SolutionTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class Solution49 implements SolutionTemplate {

    public List<List<String>> groupAnagrams(String[] strs) {
        Map<String, List<String>> result = new HashMap<>();
        for (int i = 0; i < strs.length; i++) {
            String key = getKey(strs[i]);
            List<String> mapList = result.get(key);
            if(null == mapList){
                mapList = new ArrayList<> ();
            }
            mapList.add(strs[i]);
            result.put(key, mapList);
        }
        return result.values().stream().collect(Collectors.toList());
    }

    private String getKey(String str){
        int[] albetCountArr = new int[26];
        for(int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            albetCountArr[c-97]++;
        }
        StringBuilder sb = new StringBuilder();
        for(int i = 0;i<26;i++){
            for(int j=0;j<albetCountArr[i];j++){
                sb.append((char) (i+97));
            }
        }
        return sb.toString();
    }

    @Override
    public void test() {
        String[] strs = {"eat", "tea", "tan", "ate", "nat", "bat"};
        List<List<String>> result = groupAnagrams(strs);
        System.out.println();
        return;
    }
}
